package com.summatech.gwt.mvp.client.handler;

import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Singleton;
import com.mvp4g.client.annotation.EventHandler;
import com.mvp4g.client.event.BaseEventHandler;
import com.summatech.gwt.mvp.client.MultiPresentersEventBus;
import com.summatech.gwt.mvp.client.presenter.AbstractGroupPresenter;
import com.summatech.gwt.mvp.client.presenter.ReceiverPresenter;
import com.summatech.gwt.mvp.client.presenter.SenderPresenter;

/**
 * Multiple presenter registry
 * 
 * @author Javier Ochoa
 */
@EventHandler
@Singleton
public class PresenterHandler extends BaseEventHandler<MultiPresentersEventBus> {

	private String activeEventGroup;

	public PresenterHandler() {
	}

	/**
	 * Event to kick off the presenters instantiation
	 */
	public void onCreatePresenters() {
		addWidget(SenderPresenter.class, "one");
		addWidget(SenderPresenter.class, "two");
		addWidget(SenderPresenter.class, "three");

		addWidget(ReceiverPresenter.class, "one");
		addWidget(ReceiverPresenter.class, "two");
		addWidget(ReceiverPresenter.class, "three");

		addWidget(ReceiverPresenter.class, "one");
		addWidget(ReceiverPresenter.class, "one");
		addWidget(ReceiverPresenter.class, "two");
		addWidget(ReceiverPresenter.class, "two");
		addWidget(ReceiverPresenter.class, "three");
		addWidget(ReceiverPresenter.class, "three");

		addWidget(ReceiverPresenter.class, "one");
		addWidget(ReceiverPresenter.class, "one");
		addWidget(ReceiverPresenter.class, "two");
		addWidget(ReceiverPresenter.class, "two");
		addWidget(ReceiverPresenter.class, "three");
		addWidget(ReceiverPresenter.class, "three");

		addWidget(ReceiverPresenter.class, "one");
		addWidget(ReceiverPresenter.class, "one");
		addWidget(ReceiverPresenter.class, "two");
		addWidget(ReceiverPresenter.class, "two");
		addWidget(ReceiverPresenter.class, "three");
		addWidget(ReceiverPresenter.class, "three");

		addWidget(ReceiverPresenter.class, "one");
		addWidget(ReceiverPresenter.class, "one");
		addWidget(ReceiverPresenter.class, "two");
		addWidget(ReceiverPresenter.class, "two");
		addWidget(ReceiverPresenter.class, "three");
		addWidget(ReceiverPresenter.class, "three");

	}

	/**
	 * Instatiates a new presenter and adds it to the local registry 
	 */
	private void addWidget(
			Class<? extends AbstractGroupPresenter<?, MultiPresentersEventBus>> componentClass,
			String group) {
		AbstractGroupPresenter<?, MultiPresentersEventBus> presenter = (AbstractGroupPresenter<?, MultiPresentersEventBus>) eventBus
				.addHandler(componentClass, false);
		presenter.setGroup(group);
		presenter.bind();

		eventBus.displayWidget((Widget) presenter.getView());
	}

	/**
	 * Set the active event group
	 */
	public void activateGroup(String groupToActivate) {
		this.activeEventGroup=groupToActivate;
	}

	/**
	 * Return the current active event group
	 * @return
	 */
	public String getActiveEventGroup(){
		return activeEventGroup;
	}
	
}
